package com.wondersgroup.insurance.dnuilearn.views.canvas;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;

/**
 * Created by Administrator on 2017/5/22.
 */

public class RevealDrawable extends Drawable {
    private Drawable mUnselectedDrawable;
    private Drawable mSelectedDrawable;
    private int mOrientation;
    public static final int HORIZONTAL = 1;
    public static final int VERTICAL = 2;
    private final Rect mTmpRect = new Rect();

    public RevealDrawable(Drawable unselectedDrawable, Drawable selectedDrawable, int orientation) {
        this.mUnselectedDrawable = unselectedDrawable;
        this.mSelectedDrawable = selectedDrawable;
        this.mOrientation = orientation;
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        // 绘制
        int level = getLevel();//from 0 (minimum) to 10000
        if (level == 0 || level == 10000){
            mUnselectedDrawable.draw(canvas);
        }else if (level == 5000){
            mSelectedDrawable.draw(canvas);
        }else {

            //混合效果的Drawable
            /**
             * 将画板切割成两块-左边和右边
             */
            final Rect r = mTmpRect;
            //得到当前自身Drawable的矩形区域
            Rect bounds = getBounds();

            float ratio = (level - 5000)/5000.0f;

            int w = bounds.width();
            int h = bounds.height();
            if (mOrientation == HORIZONTAL){
                w = (int) (w*Math.abs(ratio));
            }else {
                h = (int) (h*Math.abs(ratio));
            }
            int gravity = ratio < 0 ? Gravity.LEFT : Gravity.RIGHT;
            //从一个已有的bounds矩形边界范围中抠出一个矩形r
            //画左侧灰色
            Gravity.apply(
                    gravity,//从左边还是右边开始抠
                    w,//目标矩形的宽
                    h, //目标矩形的高
                    bounds, //被抠出来的rect
                    r);//目标rect

            canvas.save();
            canvas.clipRect(r);
            mUnselectedDrawable.draw(canvas);
            canvas.restore();
            //画右侧颜色的
            if(mOrientation==HORIZONTAL){
                w = getBounds().width() -w;
           }
            if(mOrientation==VERTICAL){
                h = getBounds().height()  - h;
            }
             gravity = ratio < 0 ? Gravity.RIGHT: Gravity.LEFT;
            Gravity.apply(
                    gravity,//从左边还是右边开始抠
                    w,//目标矩形的宽
                    h, //目标矩形的高
                    bounds, //被抠出来的rect
                    r);//目标rect
            canvas.save();//保存画布
            canvas.clipRect(r);//切割
            mSelectedDrawable.draw(canvas);//画
            canvas.restore();//恢复之前保存的画布
        }
    }

    @Override
    public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }


    @Override
    protected boolean onLevelChange(int level) {
        invalidateSelf();
        return true;
    }

    @Override
    public int getIntrinsicHeight() {
        return Math.max(mSelectedDrawable.getIntrinsicHeight(),
                mUnselectedDrawable.getIntrinsicHeight());
    }

    @Override
    public int getIntrinsicWidth() {
        //得到Drawable的实际宽度
        return  Math.max(mSelectedDrawable.getIntrinsicWidth(),
                mUnselectedDrawable.getIntrinsicWidth());
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
//        super.onBoundsChange(bounds);
        // 定好两个Drawable图片的宽高---边界bounds
        mUnselectedDrawable.setBounds(bounds);
        mSelectedDrawable.setBounds(bounds);
    }
}
